home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / get_cmt.arj / GET_CMT.L < prev    next >
Text File  |  1991-12-19  |  4KB  |  133 lines

  1. %{
  2.     int comments=0;
  3.     int line_numbers=0;
  4.     char *extensions[]={".C",".CPP",".CXX",".H",".HXX",".CC",NULL};
  5.     #include <string.h>
  6.     extern int yyline;
  7.     yyline=1;
  8. %}
  9.  
  10. %s COMMENT
  11. %%
  12.  
  13. <INITIAL>"/*"    {
  14.             if (line_numbers)
  15.                 fprintf(yyout,"%.4d:",yyline);
  16.             comments ++;
  17.             BEGIN COMMENT;
  18.          }
  19.  
  20. <COMMENT>.      {
  21.                     fprintf(yyout,"%c",*yytext);
  22.                 }
  23.  
  24. <COMMENT>\n     {
  25.                     yyline++;
  26.                     fprintf(yyout,"\n");
  27.                     if (line_numbers)
  28.                         fprintf(yyout,"%.4d:",yyline);
  29.                 }
  30.  
  31. <COMMENT>"*/"   {
  32.                     fprintf(yyout,"\n");
  33.                     BEGIN INITIAL;
  34.                 }
  35.  
  36. <INITIAL>"*/"   {
  37.                   fprintf(yyout,"\n\nFound close comment outside of comment.\n\n");
  38.                 }
  39.  
  40. <INITIAL>\"(\\.|[^\"])*\"       ;   /* This recognizes and ignores string constants */
  41. <INITIAL>'[^\\]'|'\\[0-0xFF]'   ;   /* This does the same for character constants   */
  42. <INITIAL>"//".*$    {
  43.                         char *line=yytext+2;
  44.                         fprintf(yyout,"%s\n",line);
  45.                     }
  46.  
  47. <INITIAL>.                      ;
  48. <INITIAL>\n                     yyline++;
  49.  
  50. %%
  51. int main(int argc,char **argv) {
  52.  
  53.     char file_name[_MAX_PATH];
  54.     char **extension;
  55.     int found=0;
  56.     char *name_end;
  57.     char output_ext[5]="\0";
  58.     char output_file[_MAX_PATH]="\0";
  59.  
  60.     while (argv[1][0]=='-') {
  61.         switch(argv[1][1]) {
  62.             case 'l':
  63.             case 'L':
  64.                     line_numbers=1;
  65.                     break;
  66.             case 'e':
  67.             case 'E':
  68.                     strcpy(output_ext,argv[1]+2);
  69.                     break;
  70.             case 'o':
  71.             case 'O':
  72.                     strcpy(output_file,argv[1]+2);
  73.                     break;
  74.             default:
  75.                     fprintf(stderr,"Usage:getcmt [-l][-?][filename [filename...]]"
  76.                     "\n-l:  add line numbers before comments"
  77.                     "\n-?:  Show this help"
  78.                     "\n filenames may include normal DOS wildcards");
  79.                     return 0;
  80.         }
  81.         argc--;
  82.         argv++;
  83.     }
  84.  
  85.     if (argc<2) {
  86.         /* read standard input if no file name supplied */
  87.         yyline=0;
  88.         yylex();
  89.     }
  90.     else while (--argc) {
  91.  
  92.         comments=0;
  93.         yyline=1;
  94.         strcpy(file_name,*++argv);
  95.         name_end=strchr(file_name,'\0');
  96.         /* otherwise, open each file supplied in turn. */
  97.         if ((yyin=fopen(file_name,"r"))==NULL && strchr(*argv,'.')==NULL) {
  98.             for (found=0,extension=extensions;*extension;extension++) {
  99.                 strcpy(name_end,*extension);
  100.                 if ((yyin=fopen(file_name,"r"))!=NULL) {
  101.                     found=1;
  102.                     break;
  103.                 }
  104.             }
  105.             if (!found) {
  106.                 fprintf(yyout,"\n\n\tError: Unable to open %s\n",*argv);
  107.                 continue;
  108.             }
  109.         }
  110.         if (*output_ext) {
  111.             strcpy(output_file,file_name);
  112.             name_end=strchr(output_file,'.')+1;
  113.             strcpy(name_end,output_ext);
  114.             if ((yyout=fopen(output_file,"a"))==NULL)
  115.                 yyout=stdout;
  116.         }
  117.         else if (*output_file) {
  118.             if ((yyout=fopen(output_file,"a"))==NULL)
  119.                 yyout=stdout;
  120.         }
  121.         fprintf(yyout,"\n\n\t%s\n",file_name);
  122.         yylex();                        /* Do the real work. */
  123.         fprintf(yyout,"\n");
  124.         yyrestart(yyin);
  125.         if (yyin !=stdin)
  126.             fclose(yyin);
  127.         fclose(yyin);
  128.         if (yyout!=stdout)
  129.             fclose(yyout);
  130.     }
  131.     return comments;
  132. }
  133.